﻿/// <reference path="AttachedHandler.js" />



function KeyPressHandler(obj, handler, binding, argList)
{
	this._boundObjKeyDown = this._obj_KeyDown.bind(this);
	this._boundObjKeyUp = this._obj_KeyUp.bind(this);
	this._boundObjTimeout = this._timeout_Callback.bind(this);
	this._boundObjInterval = this._interval_Callback.bind(this);

	this.AttachedHandler(obj, "keypress", handler, binding, argList);
}
KeyPressHandler.Inherit(AttachedHandler);

if (____a)
{
	KeyPressHandler.prototype = new AttachedHandler();
	KeyPressHandler.prototype.AttachedHandler = new AttachedHandler();

	KeyPressHandler.prototype._boundObjKeyDown = null;
	KeyPressHandler.prototype._boundObjKeyUp = null;
	KeyPressHandler.prototype._boundObjTimeout = null;
	KeyPressHandler.prototype._boundObjInterval = null;
}

KeyPressHandler._RepeatDelay = 750; // milli seconds. TO DO: detect from windows (not possible at the moment)
KeyPressHandler._RepeatSpeed = 31; // per second. TO DO: detect from windows (not possible at the moment)

KeyPressHandler.prototype._timeoutId = null;
KeyPressHandler.prototype._intervalId = null;
KeyPressHandler.prototype._event = null;

KeyPressHandler.prototype._DoAdd = function _DoAdd()
{
	Events.AddRaw(this._obj, "keydown", this._boundObjKeyDown);
	Events.AddRaw(this._obj, "keyup", this._boundObjKeyUp);
}
KeyPressHandler.prototype._Remove = function _Remove()
{
	this._Clear();

	Events.RemoveRaw(this._obj, "keydown", this._boundObjKeyDown);
	Events.RemoveRaw(this._obj, "keyup", this._boundObjKeyUp);
}
KeyPressHandler.prototype._Raise = function _Raise()
{
	this._fnToCall(this._event);
}
KeyPressHandler.prototype._Clear = function _Clear()
{
	if (this._timeoutId != null)
	{
		clearTimeout(this._timeoutId);
		this._timeoutId = null;
	}

	if (this._intervalId != null)
	{
		clearInterval(this._intervalId);
		this._intervalId = null;
	}

	this._event = null;
}
KeyPressHandler.prototype._obj_KeyDown = function _obj_KeyDown(e)
{
	if (this._event == null)
	{
		this._event = new Object();
		for (var key in e)
			this._event[key] = e[key];

		this._Raise();
		this._timeoutId = setTimeout(this._boundObjTimeout, KeyPressHandler._RepeatDelay);
	}
}
KeyPressHandler.prototype._obj_KeyUp = function _obj_KeyUp()
{
	this._Clear();
}
KeyPressHandler.prototype._timeout_Callback = function _timeout_Callback()
{
	this._timeoutId = null;

	this._intervalId = setInterval(this._boundObjInterval, 1000 / KeyPressHandler._RepeatSpeed);
}
KeyPressHandler.prototype._interval_Callback = function _interval_Callback()
{
	this._Raise();
}
